#
# @lc app=leetcode.cn id=13 lang=python
#
# [13] 罗马数字转整数
#

# @lc code=start
class Solution(object):
    def __init__(self):
        self.key_values = {
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100, 
            'D': 500,
            'M': 1000
        }

    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        length = len(s)
        result = 0
        i = 0
        while i < length:
            if i < length - 1:
                if self.key_values[s[i]] >= self.key_values[s[i+1]]:
                    result += self.key_values[s[i]]
                    i += 1
                else:
                    result += self.key_values[s[i+1]] - self.key_values[s[i]]
                    i += 2
            else:
                result += self.key_values[s[i]]
                i += 1

        return result

# @lc code=end


if __name__ == "__main__":
    solution = Solution()
    result = solution.romanToInt('MCMXCIV')
    print(result)
